EXAMPLE SOURCE CODES


     This is a small collection of mostly game example source codes. These source codes are made available to help PlayBasic programmers kick start their game programming journey. Looking for more source code / tutorials & media, then remember to visit the PlayBasic Resource board on our forums.

Found #1 item in Collisions category by Kevin Picone

 Point On Line - Line Hit Point - Point Intersect Line

By: Kevin Picone Added: October 4th, 2021

Category: All,Maths,Collisions,Intersection

    Point On Line / Line Hit Point /  Point Intersect Line

    This code defines a tLines data type that consists of four floating point numbers (x1#, y1#, x2#, y2#). It then creates an array of 100 elements of this data type called Lines.

    It then initializes each element of the Lines array by assigning random values to its x1#, y1#, x2#, and y2# fields.

    Afterwards, the code enters an infinite loop that does the following:

     Clears the screen

     Gets the current mouse position and assigns it to variables mx# and my#

     Iterates through each element lp in the Lines array and does the following:

     Assigns the x1#, y1#, x2#, and y2# fields of the current element to local variables x1#, y1#, x2#, and y2#, respectively

     Calls the Point_On_Line function, passing it mx#, my#, x1#, y1#, x2#, and y2# as arguments. The return value of this function is a boolean that indicates whether the point (mx#, my#) is on the line defined by the points (x1#, y1#) and (x2#, y2#).

     If the return value of Point_On_Line is True, it sets the current drawing color to a random RGB value.

     It then draws a line between the points (x1#, y1#) and (x2#, y2#) using the current drawing color.

     Waits for the vertical blanking interval (to prevent tearing)

     Loops back to the beginning if the space key is not pressed

    The code then defines the Point_On_Line function, which takes six floating point arguments: pointx#, pointy#, x1#, y1#, x2#, and y2#. It returns a boolean value indicating whether the point (pointx#, pointy#) is on the line defined by the points (x1#, y1#) and (x2#, y2#).

    The function first checks if the point (pointx#, pointy#) is at least parallel to the line defined by the points (x1#, y1#) and (x2#, y2#). If it is, it calculates the nearest point on the line to (pointx#, pointy#) and checks if the distance between the two points is within a certain tolerance (1 in this case). If the distance is within the tolerance, the function returns True, otherwise it returns False. If the point (pointx#, pointy#) was not parallel to the line, the function immediately returns False.

PlayBasic Code:
; PROJECT : POint On Line
; AUTHOR  : PlayBasic TUTOR
; CREATED : 4/10/2021
; EDITED  : 5/10/2021
; ---------------------------------------------------------------------

	type tlines
		x1#,y1#,x2#,y2#
	endtype

	Dim Lines(100) as tLines	

	for lp =0 to 100
		
			Lines(lp).x1 = rnd(800)
			Lines(lp).y1 = rnd(600)
			Lines(lp).x2 = rnd(800)
			Lines(lp).y2 = rnd(600)


	next

	do
		cls		
		
				mx#=mousex()
				my#=mousey()
		
				for lp =0 to 100
					
						x1#=lines(lp).x1
						y1#=lines(lp).y1
						x2#=lines(lp).x2
						y2#=lines(lp).y2
						ink -1
						if Point_On_line(mx#,my#,x1#,y1#,x2#,y2#)		
									ink rndrgb()		
						endif
						line x1#,y1#,x2#,y2#
				next

		sync		
loop spacekey()

end	




function Point_On_line(pointx#,pointy#,x1#,y1#,x2#,y2#)
	
		// compute nearest point along the line		
	   dx31#=PointX#-x1#
	   dx21#=x2#-x1#
	   dy31#=PointY#-y1#
	   dy21#=y2#-y1#
	
		l#=((dx31#*dx21#)+(dy31#*dy21#)) / ((dx21#*dx21#)+(dy21#*dy21#))
				
		// see if our point at least lies parallel with our line
		status = l#>=0 and L#<=1	
		if Status

				// if so, we compute the nearest point on the line
				x#=x1#+(dx21#*l#)
				y#=y1#+(dy21#*l#)

				// then get the distance from the line to our point.  
				Dist# = getdistance2d(x#,y#,pointx#,pointy#)

				//  check if it's within a tolerance of 1, might need
				// to be tweaked  
				Status = Dist#<=1
		
		endif
	
EndFunction status





COMMANDS USED: DIM | RND | CLS | MOUSEX | MOUSEY | INK | RNDRGB | LINE | SYNC | SPACEKEY | AND | GETDISTANCE2D |




Viewing Page [1] of [0]



Want More Source Codes?:



Release Type: The source code & tutorials found on this site are released as license ware for PlayBasic Users. No Person or Company may redistribute any file (tutorial / source code or media files) from this site, without explicit written permission.

 

 
     
 
       

(c) Copyright 2002 / 2024 Kevin Picone , UnderwareDesign.com  - Privacy Policy   Site: V0.99a [Alpha]